home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / osr5 / sco / scripts / mail / allmail < prev    next >
Encoding:
Korn shell script  |  1997-08-26  |  5.0 KB  |  200 lines

  1. #!/bin/ksh
  2. # @(#) allmail.ksh 2.1 96/11/29
  3. # 1990 john h. dubois iii (john@armory.com)
  4. # 93/04/03 read from smallest to largest.
  5. # 93/07/01 Use ksh sorting to avoid need to invoke sort.
  6. #          Use command line args & rcfile.
  7. # 93/11/07 Print names of mailboxes remaining before each mail invokation
  8. # 93/11/22 Print remaining mailboxes without $MAILDIR
  9. # 95/08/07 Added t option.
  10. # 96/02/07 Adding setting of LESS prompt.
  11. # 96/11/29 Let folders begin with 'From ' in addition to ^A^A^A^A
  12. #          Added .mushfilter capability
  13.  
  14. name=${0##*/}
  15. rcname=.allmail
  16. rcfile=$HOME/$rcname
  17. SetPrompt=true
  18. if [ -f $rcfile ]; then
  19.     . $rcfile || exit 1
  20. fi
  21. [ -z "$MAILDIR" ] && MAILDIR=$HOME/mail
  22. [ -z "$MAILPROG" ] && MAILPROG="mail -f"
  23.  
  24. typeset -i notfolders=0 boxnum=0
  25.  
  26. alias istrue="test 0 -ne"
  27. alias isfalse="test 0 -eq"
  28. List=false
  29.  
  30. Usage="Usage: $name [-hnt] [-d<maildir>] [-p<mailprog>] [folder|file ...]"
  31.  
  32. while getopts d:hnp:t opt; do
  33.     case $opt in
  34.     h)
  35.     echo \
  36. "$name: read all mail folders.
  37. $Usage
  38. $name is used to read a set of mail folders.  It reads initialization
  39. information from the environment and its rcfile, \$HOME/$rcname.  Folders are
  40. read in order of their size, smallest first.  Folders that are empty or are
  41. not mail folders are skipped.  A file is considered a mail folder if the first
  42. line is either ^A^A^A^A (four control-A characters, as used by mailx) or starts
  43. with the word 'From'.
  44. If the mail program being used is \"mush\" and the file \".mushfilter\" exists
  45. in the invoking user's home directory, mush is told to source that file after
  46. reading a folder.  This allows filtering commands to be executed on the folder.
  47. The following variables can be set in the environment or in the rcfile.
  48. Values in the environment override those in the rcfile.  Options given on
  49. the command line override the variables.
  50. MAILPROG: A program to use to read mail instead of the default, \"mail -f\".
  51. MAILDIR: Where to find mail folders given with relative paths, \$HOME/mail
  52. by default.
  53. FOLDERS: The list of folders to read, if none are given on the command line.
  54. NOTFOLDERS: The folder list is set to be all of the files in MAILDIR that
  55. are mail folders and are not given in NOTFOLDERS.
  56. If no folders are given on the command line and neither FOLDERS nor NOTFOLDERS
  57. is set, the behaviour is as for NOTFOLDERS except that no folders are skipped.
  58. Options:
  59. -d<maildir>: <maildir> overrides the value of MAILDIR.
  60. -h: Print this help.
  61. -n: The files named on the command line are files that should be skipped,
  62.     as with NOTFOLDERS.
  63. -p<mailprog>: <mailprog> overrides the value of MAILPROG.
  64. -t: Show the files that would be read, but do not read them."
  65.        exit 0
  66.        ;;
  67.     d)
  68.     MAILDIR=$OPTARG
  69.     ;;
  70.     n)  notfolders=1
  71.     ;;
  72.     t)  List=true
  73.     ;;
  74.     p)
  75.     MAILPROG=$OPTARG
  76.     ;;
  77.     +?)
  78.     echo "$name: options should not be preceded by a '+'."
  79.     exit 1
  80.     ;;
  81.     ?) 
  82.     echo "Use -h for help."
  83.     exit 1
  84.     ;;
  85.     esac
  86. done
  87.  
  88. # remove args that were options
  89. let OPTIND=OPTIND-1
  90. shift $OPTIND
  91.  
  92. [ -z "$MAILDIR" ] && MAILDIR=$HOME/mail
  93. if cd "$MAILDIR"; then :; else
  94.     echo "$name: $MAILDIR: no such directory."
  95.     exit 1
  96. fi
  97.  
  98. if isfalse notfolders; then
  99.     [ $# -eq 0 ] && set -- $FOLDERS
  100.     if [ $# -eq 0 -a -n "$NOTFOLDERS" ]; then
  101.     set -- $NOTFOLDERS
  102.     notfolders=1
  103.     fi
  104. fi
  105.  
  106. if istrue notfolders || [ $# -eq 0 ]; then
  107.     if istrue notfolders; then
  108.     OIFS=$IFS
  109.     IFS=\|
  110.     set -- "$@"
  111.     NotPat="@($*)"
  112.     IFS=$OIFS
  113.     fi
  114.     [ -z "$NotPat" ] && NotPat=.
  115.     FOLDERS=
  116.     for file in *; do
  117.     if eval \
  118.     [[ -f '"$file" && -r "$file" && -s "$file" && "$file"' != "$NotPat" ]];
  119.     then
  120.         read -r < "$file"
  121.         if [[ "$REPLY" =  || "$REPLY" = "From "* ]]; then
  122.         FOLDERS="$FOLDERS $file"
  123.         fi
  124.     fi
  125.     done
  126.     set -- $FOLDERS
  127. fi
  128.  
  129.  
  130. if [ $# -eq 0 ]; then
  131.     print "No mail folders found."
  132.     exit 0
  133. fi
  134.  
  135. typeset -Z9 size
  136. l -d "$@" 2>/dev/null |
  137. while read line; do
  138.     set -- $line
  139.     [ $# -eq 0 ] && continue
  140.     size=$5
  141.     shift $(($#-1))
  142.     file=$1
  143.     # Make filenames absolute so they'll be found when we cd back to orig dir
  144.     [[ "$file" != /* ]] && file="$MAILDIR/$file"
  145.     # Make sort be a list of space-separated size,mailboxname pairs,
  146.     # with elements of the list separated by newlines
  147.     sort="$sort
  148. $size $file"
  149. done
  150.  
  151. cd -
  152.  
  153. # Set IFS to newline-only so pairs will be sorted together
  154. IFS="
  155. "
  156. set -- $sort
  157. set -s
  158.  
  159. # Set IFS to space so size & mailboxname can be separated
  160. IFS=" "
  161. set -- $*
  162.  
  163. # Pick out the mailbox names
  164. while [ $# -gt 0 ]; do
  165.     Mailboxes[boxnum]=$2
  166.     let boxnum+=1
  167.     shift 2
  168. done
  169.  
  170. set -- "${Mailboxes[@]}"
  171.  
  172. if $List; then
  173.     l -- "$@"
  174.     exit 0
  175. fi
  176.  
  177. # If LESS is not set or does not include prompt, add it.
  178. if $SetPrompt; then
  179.     case "$LESS" in
  180.     "") export LESS='-P(%pB\%)';;
  181.     *P*) ;;
  182.     *) export LESS="${LESS}P(%pB\%)";;
  183.     esac
  184. fi
  185.  
  186. [[ "$MAILPROG" = mush* && -f "$HOME/.mushfilter" ]] &&
  187. filt="-F $HOME/.mushfilter" || filt=
  188.  
  189. lessP=$LESS
  190. while [ $# -gt 0 ]; do
  191.     echo -n "Remaining: "
  192.     for mailfile; do
  193.     print -n -- "${mailfile##$MAILDIR/} "
  194.     done
  195.     echo ""
  196.     $SetPrompt && lessP="$LESS [${1##*/}]"
  197.     LESS=$lessP $MAILPROG "$1" $filt
  198.     shift
  199. done
  200.